package com.xms.schema.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.schema.api.model.UpdateEntityModel;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.schema.service.entity.IEntityUpdaterService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import com.xms.web.framework.model.SetStateModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.Arrays;

/**
 * <p>
 * EntityUpdaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/20
 */
@RestController
@RequestMapping("{org}/api/schema/entity")
public class EntityUpdaterController extends ApiBaseController {
    private IEntityFinderService _entityFinderService;
    private IEntityUpdaterService _entityUpdaterService;

    @Autowired
    public void EntityUpdaterController(IEntityFinderService entityFinderService,
                                        IEntityUpdaterService entityUpdaterService) {
        _entityFinderService = entityFinderService;
        _entityUpdaterService = entityUpdaterService;
    }

    @ApiOperation("修改实体")
    @PostMapping("update")
    public JsonResultObject update(@RequestBody UpdateEntityModel model) throws XmsException, SQLException {
        if (UUIDUtil.isEmpty(model.entityid)) {
            return JNotSpecifiedParams("entityid");
        }
        if (StringUtil.isEmpty(model.localizedname) || StringUtil.isEmpty(model.localizedname)) {
            return JNotSpecifiedParams("localizedname");
        }
        Entity entity = _entityFinderService.getById(model.entityid);
        entity.setEntitymask(model.entitymask);
        entity.setAuthorizationenabled(model.authorizationenabled);
        entity.setDuplicateenabled(model.duplicateenabled);
        entity.setLocalizedname(model.localizedname);
        entity.setLogenabled(model.logenabled);
        entity.setWorkflowenabled(model.workflowenabled);
        entity.setDescription(model.description);
        if (CollectionUtil.notEmpty(model.entitygroupid))
            entity.setEntitygroups(JsonUtil.toJSONString(model.entitygroupid));
        else
            entity.setEntitygroups("");
        return _entityUpdaterService.update(entity) ? JUpdateSuccess() : JUpdateFailure();
    }

    @ApiOperation("设置实体授权状态")
    @PostMapping("setauthstate")
    public JsonResultObject setAuthorizationState(@RequestBody SetStateModel model) {
        if (CollectionUtil.isEmpty(model.recordid)) {
            return JNotSpecifiedParams("recordid");
        }
        return _entityUpdaterService.updateAuthorization(model.state, Arrays.asList(model.recordid)) ? JUpdateSuccess() : JUpdateFailure();
    }

    @ApiOperation("设置实体日志状态")
    @PostMapping("setlogstate")
    public JsonResultObject setLoggingState(@RequestBody SetStateModel model) {
        if (CollectionUtil.isEmpty(model.recordid)) {
            return JNotSpecifiedParams("recordid");
        }
        return _entityUpdaterService.updateLogEnabled(model.state, Arrays.asList(model.recordid)) ? JUpdateSuccess() : JUpdateFailure();
    }
}
